<template>
  <div class="login-page">
    <el-card>
      <template #header>黑马面经运营后台</template>
      <el-form
        ref="form"
        :model="user"
        :rules="rules"
        autocomplete="off"
        label-width="60px"
      >
        <el-form-item prop="username">
          <el-input v-model="user.username" placeholder="输入用户名"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="user.password"
            type="password"
            placeholder="输入用户密码"
          ></el-input>
        </el-form-item>

        <el-form class="tc">
          <el-button type="primary" @click="onSubmit">登 录</el-button>
          <el-button>重 置</el-button>
        </el-form>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
import { Message } from 'element-ui'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      // 注意，给 <el-form> 加入ref属性，下面的代码自行修改
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 通过验证
          const res = await loginAPI(this.user)
          console.log(res)
          // console.log(this.user)
          // 存储Token
          this.$store.commit('user/updateToken', res.data.token)
          Message.success('登陆成功')
          this.$router.push('/')
        } else {
          // 验证失败
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
// sass语法演示  （stylus和sass语法一样）：
// $color: blue
// div
//   background-color: $color

// scss语法演示：
// $color: pink;
// div {
//   background-color: $color;
// }

// Element-UI 中，标签名，就是类名

.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
